<template>
  <div>
    <Header :title="title"></Header>
    <Card
        v-for="item in list"
        :key="item.id"
        :id="item.id"
        :desc="item.desc"
    ></Card>

    <van-empty v-if="list.length===0" description="描述文字" />
  </div>


</template>

<script>
import Header from "../components/Header";
import Card from "../components/Card";
import {searchNameById} from "../utils/api";
import {Dialog} from "vant";
import {Bus} from "../bus";
export default {
  name: "InfoDetail",
  components: {Card, Header},
  data(){
    return {
      title: '123',
      list:[
        /*{id:1,desc:'中国11'},
        {id:2,desc:'中国22'},
        {id:3,desc:'中国33'},
        {id:4,desc:'中国44'},
        {id:5,desc:'中国55'},*/
      ]
    }
  },
  created() {
    this.beforeHttp()
    console.log(this.$route.params)
    this.init()
    this.getNameById()

    Bus.$on('delete-card',(id,desc)=>{
      console.log('删除vue中的list')
      console.log(id,desc)
      function beforeClose(action, done) {
        if (action === 'confirm') {
          setTimeout(done, 300);
        } else {
          done();
        }
      }

      this.list.some((item,index)=>{
        if(id===item.id){
          Dialog.confirm({
            title: '删除',
            message: '删除留言'+item.id,
            beforeClose,
          }).then(res=>{
            //删除单个信息
            this.list.splice(index,1)
          }).catch(res=>{
            console.log(res)
          });


        }
      })
    })
  },
  methods:{
    beforeHttp(){
      console.log(this.$store.getters.getLogin)
      if(this.$store.getters.getLogin == ''){
        this.$router.push('/')
      }
    },
    init(){
      this.title = '写给' + this.$route.params.name + '的留言'

    },
    async getNameById(){
      const id = this.$store.getters.getUserInfo.id
      const name = this.$route.params.name
      const {data:res} = await searchNameById(id,name)

      console.log(res.data)

      this.list = []

      res.data.forEach((item,index)=>{
        const say = {}
        say.id = index + 1
        say.desc = item.description
        this.list.push(say)
      })

    },
    success(){
      Dialog.alert({
        title: '消息',
        message: '删除成功',
        theme: 'round-button',
      }).then(() => {
        // on close
      });
    },
  }
}
</script>

<style scoped>

</style>